package manager;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.DBConnector;

/**
 * Servlet implementation class GetXSL
 */
public class GetXSL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetXSL() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getSession(false).getAttribute("username").toString();
		String xslName = request.getParameter("xslName");
		DBConnector dbConn;
		
		try {
			if (xslName.equals("Default")){
				response.sendRedirect("sched.xslt");
				return;
			}
			else if (xslName.equals("Custom")){
				response.sendRedirect("sched2.xslt");
				return;
			}

			dbConn = new DBConnector();
			PreparedStatement stmnt = dbConn.getConnection().prepareStatement(
					"SELECT xslt " +
					"FROM user_stylesheets " +
					"WHERE username = ? " +
					"and style_name=?; ");
			stmnt.setString(1, username);
			stmnt.setString(2,xslName );
		
			ResultSet res = stmnt.executeQuery();
			res.next();
			response.getOutputStream().print(res.getString(1));
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
